/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.test;

import org.mockito.Mockito;

import io.sentry.GsonSerializer;
import io.sentry.SentryEnvelope;
import io.sentry.SentryEvent;
import io.sentry.SentryOptions;
import io.sentry.protocol.SentryTransaction;

/**
 * Assertions
 *
 * @since 2021-07-01
 */
public class Assertions {
    private Assertions() {
    }

    /**
     * Verifies is [SentryEnvelope] contains first event matching a predicate.
     *
     * @param callback a callback for predicate
     * @return SentryEnvelope
     */
    public static SentryEnvelope checkEvent(SentryEventPredicateCallback callback) {
        SentryOptions options = new SentryOptions();
        options.setSerializer(new GsonSerializer(new SentryOptions()));
        return Mockito.argThat(envelope -> {
            SentryEvent event = null;
            try {
                event = envelope.getItems().iterator().next().getEvent(options.getSerializer());
            } catch (Exception e) {
                e.getMessage();
            }
            if (event != null) {
                if (callback != null) {
                    callback.predicate(event);
                }
                return true;
            } else {
                throw new AssertionError("event is null");
            }
        });
    }

    /**
     * check Transaction
     *
     * @param callback a callback for predicate
     * @return SentryEnvelope
     */
    public static SentryEnvelope checkTransaction(SentryTransactionPredicateCallback callback) {
        SentryOptions options = new SentryOptions();
        options.setSerializer(new GsonSerializer(new SentryOptions()));
        return Mockito.argThat(envelope -> {
            SentryTransaction transaction = null;
            try {
                transaction = envelope.getItems().iterator().next().getTransaction(options.getSerializer());
            } catch (Exception e) {
                e.getMessage();
            }
            if (transaction != null) {
                if (callback != null) {
                    callback.predicate(transaction);
                }
                return true;
            } else {
                throw new AssertionError("transaction is null");
            }
        });
    }

    /**
     * SentryEventPredicateCallback
     *
     * @since 2021-07-01
     */
    public interface SentryEventPredicateCallback {
        /**
         * predicate sentryEvent
         *
         * @param sentryEvent for predicate
         */
        void predicate(SentryEvent sentryEvent);
    }

    /**
     * SentryTransactionPredicateCallback
     *
     * @since 2021-07-01
     */
    public interface SentryTransactionPredicateCallback {
        /**
         * predicate sentryTransaction
         *
         * @param sentryTransaction for predicate
         */
        void predicate(SentryTransaction sentryTransaction);
    }
}
